package edu.elon.cs.mobile.ukulele;

import java.util.HashMap;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.widget.ImageView;

public class Chord extends ImageView{
	
	private String name;
	private Dot[] dots;
	private int strum;
	private int chord;
	private SoundPool soundPool;
	private HashMap<Integer, Integer> soundPoolMap;
	private HashMap<Integer, Integer> soundPoolMapSTRUM;
	public static final int CHORD = 1;
	public static final int STRUM = 1;
	private Context context;
	private int image;
	
	public Chord(String name, Context context, int strum, int chord, int image, Dot...dots) {
		super(context);
		this.name = name;
		this.dots = dots;
		this.strum = strum;
		this.chord = chord;
		this.image = image;
		this.context = context;
		soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
		soundPoolMap = new HashMap<Integer, Integer>();
		soundPoolMapSTRUM = new HashMap<Integer, Integer>();
		soundPoolMap.put(CHORD, soundPool.load(context, chord, 1));
		soundPoolMapSTRUM.put(STRUM, soundPool.load(context, strum, 1));
		this.setImageResource(image);
	}
	
	public Chord(Chord chord){
		this(chord.name, chord.context, chord.strum, chord.chord, chord.image, chord.dots);
	}
	
	public void playSoundChord(int sound) {
	    /* Updated: The next 4 lines calculate the current volume in a scale of 0.0 to 1.0 */
	    AudioManager mgr = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
	    float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
	    float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);    
	    float volume = streamVolumeCurrent / streamVolumeMax;
	    
	    /* Play the sound with the correct volume */
	    soundPool.play(soundPoolMap.get(sound), volume, volume, 1, 0, 1f);     
	}
	
	public void playSoundStrum(int sound) {
	    /* Updated: The next 4 lines calculate the current volume in a scale of 0.0 to 1.0 */
	    AudioManager mgr = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
	    float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
	    float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);    
	    float volume = streamVolumeCurrent / streamVolumeMax;
	    
	    /* Play the sound with the correct volume */
	    soundPool.play(soundPoolMapSTRUM.get(sound), volume, volume, 1, 0, 1f);     
	}

	public void playChord() {
	    playSoundChord(CHORD);
	} 
	
	public void playStrum() {
	    playSoundStrum(STRUM);
	} 

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Dot[] getDots() {
		return dots;
	}

	public void setDots(Dot[] dots) {
		this.dots = dots;
	}
	
	public int getImage() {
		return this.image;
	}

}
